package org.budo.cmbc.sdk.entity;

import java.io.Serializable;

import org.budo.xml.Xml;
import org.budo.xml.XmlObject;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Getter
@Setter
@Slf4j
public class AbstractCmbcResponse implements Serializable {
    private static final long serialVersionUID = -6544733965965350249L;

    private String responseBody;

    public String getMessage() {
        XmlObject response = Xml.parseString(this.responseBody);
        XmlObject cmbc = response.getChildNodeByName("CMBC");

        if (null == cmbc) {
            log.error("#29 getMessage, cmbc is null, responseBody=" + this.responseBody);
            return null;
        }

        XmlObject responseHeader = cmbc.getChildNodeByName("responseHeader");
        XmlObject status = responseHeader.getChildNodeByName("status");
        String message = status.getChildNodeTextContentByName("message");
        return message;
    }

    protected XmlObject xDataBody(String responseBody) {
        XmlObject response = Xml.parseString(responseBody);
        XmlObject cmbc = response.getChildNodeByName("CMBC");

        if (null == cmbc) {
            throw new RuntimeException("#27 " + responseBody);
        }

//        XmlObject responseHeader = cmbc.getChildNodeByName("responseHeader");
//        XmlObject status = responseHeader.getChildNodeByName("status");
//        String message = status.getChildNodeTextContentByName("message");

//        if (StringUtil.equals("不支持此业务!", message)) {
//            log.error("#37 message=" + message + ", responseBody=" + responseBody);
//            return null;
//        }

        // 接口报错
//        if (!StringUtil.equals("ok", message) && !StringUtil.equals("无记录", message)) {
//            log.error("#43 message=" + message + ", responseBody=" + responseBody);
//            return null;
//        }

        // 正常
        XmlObject xDataBody = cmbc.getChildNodeByName("xDataBody");
        return xDataBody;
    }
}